//
//  TTMusicFindCollectionCell.m
//  TTMusic
//
//  Created by 马金晖 on 16/3/31.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TTMusicFindCollectionCell.h"
#import "TTMusicDiscoveryPageFirstCollectionVIewCell.h"
#import "TTMusicDiscoveryPageSecondCollectionViewCell.h"
#import "TTMusicDiscoveryPageThirdCollectionViewCell.h"
#import "TTMusicDiscoveryPageForthCollectionViewCell.h"
#import "TTMusicDiscoveryPageFifthCollectionViewCell.h"
#import "CPYHeaderViewCollectionViewCell.h"
#import "CPYDiscoveryPageDataDetailModel.h"
#import "CPYDiscoveryPageDataModel.h"
#import "CPYDiscoveryPageSongListModel.h"
#import "TTMusicAVPlayer.h"
#import "CPYDiscoveryPageSongListModel.h"

@interface TTMusicFindCollectionCell ()
@property (nonatomic, strong) CPYSONGSModel *songsModel;
@property (nonatomic, strong) NSMutableArray *songsDataSource;
@property (nonatomic, strong) CPYDiscoveryPageSongListModel *songsDetailModel;
@end

@implementation TTMusicFindCollectionCell


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSongsModel];
        self.songsDataSource = [NSMutableArray arrayWithCapacity:0];
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.collectionView = [[TTMusicBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, SHORTHEIGHT) collectionViewLayout:_flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[TTMusicDiscoveryPageFirstCollectionVIewCell class] forCellWithReuseIdentifier:@"TTMusicDiscoveryPageFirstCollectionVIewCellIdentifier"];
        [_collectionView registerClass:[TTMusicDiscoveryPageSecondCollectionViewCell class] forCellWithReuseIdentifier:@"TTMusicDiscoveryPageSecondCollectionViewCellIdentifier"];
        [_collectionView registerClass:[TTMusicDiscoveryPageThirdCollectionViewCell class] forCellWithReuseIdentifier:@"TTMusicDiscoveryPageThirdCollectionViewCellIdentifier"];
        [_collectionView registerClass:[TTMusicDiscoveryPageForthCollectionViewCell class] forCellWithReuseIdentifier:@"TTMusicDiscoveryPageForthCollectionViewCellIdentifier"];
        [_collectionView registerClass:[TTMusicDiscoveryPageFifthCollectionViewCell class] forCellWithReuseIdentifier:@"TTMusicDiscoveryPageFifthCollectionViewCellIdentifier"];
        [_collectionView registerClass:[CPYHeaderViewCollectionViewCell class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewForCollection"];
        [self.contentView addSubview:_collectionView];
    }
    return self;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        TTMusicDiscoveryPageFirstCollectionVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicDiscoveryPageFirstCollectionVIewCellIdentifier" forIndexPath:indexPath];
        CPYDiscoveryPageDataModel *dataModel = _dataSource[indexPath.item];
        NSMutableArray *mutable = [NSMutableArray array];
        for (CPYDiscoveryPageDataDetailModel *dataDetailModel in dataModel.data) {
            [mutable addObject:dataDetailModel.picUrl];
        }
        cell.imageArray = [NSMutableArray arrayWithArray:mutable];
        return cell;
    }else if (indexPath.section == 1) {
        TTMusicDiscoveryPageSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicDiscoveryPageSecondCollectionViewCellIdentifier" forIndexPath:indexPath];
        CPYDiscoveryPageDataModel *dataModel = _dataSource[indexPath.section];
         CPYDiscoveryPageDataDetailModel *dataDetailModel = dataModel.data[indexPath.item];
        cell.dataDetailModel = dataDetailModel;
        return cell;
    }else if(indexPath.section == 2) {
        TTMusicDiscoveryPageThirdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicDiscoveryPageThirdCollectionViewCellIdentifier" forIndexPath:indexPath];
        CPYDiscoveryPageDataModel *dataModel = _dataSource[indexPath.section];
        cell.num = indexPath.item + 1;
        CPYDiscoveryPageDataDetailModel *dataDetailModel = dataModel.data[0];
        CPYDiscoveryPageSongListModel * songListModel = dataDetailModel.songs[indexPath.item];
        cell.songListModel = songListModel;
        return cell;
    }else if (indexPath.section == 3 || indexPath.section == 4 || indexPath.section == 6 || indexPath.section == 7) {
        TTMusicDiscoveryPageForthCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicDiscoveryPageForthCollectionViewCellIdentifier" forIndexPath:indexPath];
        CPYDiscoveryPageDataModel *dataModel = _dataSource[indexPath.section];
        CPYDiscoveryPageDataDetailModel *dataDetailModel = dataModel.data[indexPath.item];
        cell.dataDetailModel = dataDetailModel;
        return cell;
    }else {
        TTMusicDiscoveryPageFifthCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicDiscoveryPageFifthCollectionViewCellIdentifier" forIndexPath:indexPath];
        CPYDiscoveryPageDataModel *dataModel = _dataSource[indexPath.section];
        CPYDiscoveryPageDataDetailModel *dataDetailModel = dataModel.data[indexPath.item];
        cell.dataDetailModel = dataDetailModel;
        return cell;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 4;
    }else if (section == 2){
        return 3;
    }else if (section == 3){
        return 6;
    }else if (section == 4){
        return 3;
    }else if (section == 5){
        return 3;
    }else if (section == 6){
        return 3;
    }else if (section == 7){
        return 3;
    }else if (section == 8){
        return 3;
    }else {
        return 1;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(SysWidth, 190 * FitHeight);
    }else if(indexPath.section == 1){
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth / 4 , 80 * FitHeight);
    }else if(indexPath.section == 2) {
        return CGSizeMake(SysWidth , 80 * FitHeight);
    }else if(indexPath.section == 3) {
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth / 3 , 185 * FitWidth);
    }else if(indexPath.section == 4) {
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth / 3 , 185 * FitHeight);
    }else if(indexPath.section == 5) {
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth , 80 * FitHeight);
    }else if(indexPath.section == 6) {
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth / 3 , 185 * FitHeight);
    }else if(indexPath.section == 7) {
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth / 3 , 185 * FitHeight);
    }else if(indexPath.section == 8) {
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth , 80 * FitHeight + 15);
    }else {
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        return CGSizeMake(SysWidth, 80 * FitHeight + 15);
    }
}
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (indexPath.section > 1) {
        if (kind == UICollectionElementKindSectionHeader)
        {
            CPYHeaderViewCollectionViewCell *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewForCollection" forIndexPath:indexPath];
            if (indexPath.section == 7) {
                [headerView.moreButton setTitle:@"更多>" forState:UIControlStateNormal];
            }
            headerView.dataModel = _dataSource[indexPath.section];
            reusableview = headerView;
        }
    }
    return reusableview;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section==0||section==1) {
        return CGSizeZero;
    }
    return CGSizeMake(SysWidth, 30);
}
- (void)setDataSource:(NSMutableArray *)dataSource {
    _dataSource = dataSource;
    [_collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        [self.delegate jumpWithItem:indexPath.item withSection:indexPath.section];
    }
    if (indexPath.section == 2) {
//        播放
        [TTMusicAVPlayer shareTTMusicAVPlayer].local = indexPath.item;
        [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
    }
    if (indexPath.section == 3 || indexPath.section == 4 || indexPath.section == 6 || indexPath.section == 7) {
        CPYDiscoveryPageDataModel *dataModel = _dataSource[indexPath.section];
        CPYDiscoveryPageDataDetailModel *dataDetailModel = dataModel.data[indexPath.item];
        [self.delegate jumpWithItem:indexPath.item withSection:indexPath.section withSongId:dataDetailModel.actionModel.value];
    }
    
}
- (void)creatSongsModel {
    [TTMusicAFNetworkTool GETNetWithUrl:@"http://api.favorite.ttpod.com/favorite/song/plaza?random=0&version=0&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.88017379379392&longtitude=121.5416356872621" body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        self.songsModel = [[CPYSONGSModel alloc] initWithDataSource:result[@"data"]];
        for (_songsDetailModel in _songsModel.songs) {
//            NSLog(@"%@", _songsModel.songs);
        }
//        CPYDiscoveryPageSongListModel *songsModel
        [_collectionView reloadData];
    } failure:^(NSError *error) {
    }];
}


@end
